﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class VacationAdd : System.Web.UI.Page
    {
        protected EmpBasicInfo ebi;
        protected void Page_Load(object sender, EventArgs e)
        {
            ebi = Session["ebi"] as EmpBasicInfo;
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string sql = string.Format(@"select top 3 b.Id,b.FormNo,b.PreStartTime,b.PreEndTime,a.ReplacedDay Remarks  from ReqVacationMains a,ReqVacationSubs b where a.FormNo=b.FormNo and a.EmpId={0} and ReqDate>=DATEADD(M,-6,GETDATE()) and a.VacationType='调休' and (a.StatusId=2 or a.StatusId=5) order by PreStartTime desc;", ebi.Id);
            IEnumerable<ReqVacationSub> reqVacationSubs= ctx.ExecuteQuery<ReqVacationSub>(sql);
            rptList.DataSource = reqVacationSubs;
            rptList.DataBind();

            sql = string.Format(@"select top 3 * from ReqOvertime
  where EmpId={0} and ReqDate>=DATEADD(M,-6,GETDATE())
  order by FormNo desc;", ebi.Id);
            IEnumerable<ReqOvertime> reqOvertimes = ctx.ExecuteQuery<ReqOvertime>(sql);
            rptList2.DataSource = reqOvertimes;
            rptList2.DataBind();

            sql = string.Format(@"select top 3 * from duty
  where EmpId={0} and dutydate>=DATEADD(M,-6,GETDATE())
  order by id desc;", ebi.Id);
            IEnumerable<Duty> duty = ctx.ExecuteQuery<Duty>(sql);
            rptList3.DataSource = duty;
            rptList3.DataBind();

            AnnualLeaves a = ctx.AnnualLeaves.First(q => q.empid == ebi.Id);
            int nianJiaTotal = a.annualwork;
            lblNianJiaTotal.InnerText = nianJiaTotal.ToString();

            sql = string.Format(@"SELECT ISNULL(SUM(DaysCnt),0)
  FROM ReqVacationMains a,ReqVacationSubs b
  where a.FormNo=b.FormNo and a.EmpId={0} and VacationType='年假' and b.PreStartTime>='{1}' and StatusId=2;", ebi.Id.ToString(), DateTime.Today.ToString("yyyy-01-01"));
            lblNianJia.InnerText = ctx.ExecuteQuery<decimal>(sql).First().ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqVacationMain reqVacationMain = new ReqVacationMain();
            string maxFormNo = ctx.ReqVacationMains.Max(r => r.FormNo);
            string formNo = "Form_Vaca_000001";
            if (!string.IsNullOrEmpty(maxFormNo))
            {
                formNo = "Form_Vaca_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
            }
            reqVacationMain.FormNo = formNo;
            reqVacationMain.ReqDate = DateTime.Now;
            reqVacationMain.EmpId = ebi.Id;
            reqVacationMain.VacationType = VacationType.Value;
            reqVacationMain.DaysCnt = decimal.Parse(DaysCnt.Value);
            reqVacationMain.ReplacedDay = ReplacedDay.Value;
            //reqVacationMain.Cause = Cause.Value;
            ctx.ReqVacationMains.InsertOnSubmit(reqVacationMain);
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + reqVacationMain.FormNo + "';window.close();</script>");
        }
    }
}